package com.example.mobwe_project;
 
import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
 
public class ImageAdapter extends BaseAdapter implements ListAdapter{
    private Context mContext;
    
    private ArrayList<ImageView> _thumbnails;
    private ArrayList<String> _imagesNames;
    private ArrayList<Ebook> _ebooks;
 
    // Constructor
    public ImageAdapter(Context c){
    	Log.d("ImageAdapter","Creating an image adapter");
        this.mContext = c;
    	Log.d("ImageAdapter","Image adapter was made");
    }
 
    @Override
    public int getCount() {
        return this._thumbnails.size();
    }
 
    @Override
    public Object getItem(int position) {
        return this._thumbnails.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return 0;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	Log.d("ImageAdapter","Method getView was called");
    	try{
	    	ImageView imageView;
	        if (convertView == null) {
	            imageView = new ImageView(this.mContext);
	            imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
	            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
	            imageView.setPadding(5, 5, 5, 5);
	        } else {
	            imageView = (ImageView) convertView;
	        }
	        imageView.setImageDrawable(_thumbnails.get(position).getDrawable());
	    	Log.d("ImageAdapter","Method getView going to return");
	        return imageView;
    	} catch (Exception e){
	    	Log.d("ImageAdapter","Something went wrong");
	    	return new ImageView( this.mContext );
    	}
    }
    
    public void loadThumbnails() {
    	Log.d("ImageAdapter","Method loadThumbnails was called");
        // Init the ArrayList :
        this._thumbnails = new ArrayList<ImageView>();
        this._imagesNames = new ArrayList<String>();        

    	Log.d("ImageAdapter","Creating a new ebook collection");
        EbookCollection ebookCollection = new EbookCollection();
        
        this._ebooks = ebookCollection.getEbooks();

    	Log.d("ImageAdapter","Fetching the preview images");
        ArrayList<File> imagesThumbnails = ebookCollection.getPreviewImages();
    	Log.d("ImageAdapter","Preview images were fetched");

    	Log.d("ImageAdapter","Starting to foreach the thumbnails");
        for(File imageThumbnail : imagesThumbnails)
        {
            ImageView imageView = new ImageView( this.mContext );

        	Log.d("ImageAdapter","a");
            imageView.setImageDrawable(Drawable.createFromPath(imageThumbnail.getAbsolutePath()));
        	Log.d("ImageAdapter","b");
            this._thumbnails.add(imageView);
        	Log.d("ImageAdapter","c");
            this._imagesNames.add(imageThumbnail.getName());
        	Log.d("ImageAdapter","d");
        }
    	Log.d("ImageAdapter",this._thumbnails.size() + " thumbnails was loaded");
    	Log.d("ImageAdapter","Loading the thumbnails was finished");
    } 
    public ArrayList<Ebook> getEbooks(){
    return this._ebooks;
}
}
